package com.unionftech.common.utils

import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.StringRes
import com.google.i18n.phonenumbers.PhoneNumberUtil
import com.unionftech.common.CommonConstants
import com.unionftech.common.common.language.DynamicResourceManager

object CheckInputUtil {
    @JvmStatic
    fun checkInput(
        input: String,
        regex: String,
        inputView: EditText,
        errorView: TextView,
        @StringRes hintResId: Int,
        @StringRes errorResId: Int
    ): Boolean {
        var result = true
        if (TextUtils.isEmpty(input)) {
            inputView.isSelected = true
            errorView.visibility = View.VISIBLE
            errorView.text =
                DynamicResourceManager.getString(errorView.context, hintResId)
            result = false
        } else {
            if (RegexUtils.match(input, regex)) {
                errorView.visibility = View.INVISIBLE
                inputView.isSelected = false
            } else {
                errorView.visibility = View.VISIBLE
                inputView.isSelected = true
                errorView.text =
                    DynamicResourceManager.getString(errorView.context, errorResId)
                result = false
            }
        }
        return result
    }

    @JvmStatic
    fun checkInputWithoutTip(
        input: String,
        regex: String,
    ): Boolean {
        var result = true
        if (TextUtils.isEmpty(input)) {
            result = false
        } else if (!RegexUtils.match(input, regex)) {
            result = false
        }
        return result
    }

    @JvmStatic
    fun checkNotEmptyInput(
        input: String,
        inputView: EditText,
        errorView: TextView,
        @StringRes errorResId: Int
    ): Boolean {
        var result = true
        if (TextUtils.isEmpty(input)) {
            inputView.isSelected = true
            errorView.visibility = View.VISIBLE
            errorView.text =
                DynamicResourceManager.getString(errorView.context, errorResId)
            result = false
        } else {
            inputView.isSelected = false
            errorView.visibility = View.INVISIBLE
        }
        return result
    }

    @JvmStatic
    fun checkPinYinInput(
        input: String,
        countryCode: String,
        inputView: EditText,
        errorView: TextView,
        @StringRes hintResId: Int,
        @StringRes errorResId: Int,
    ): Boolean {
        var result = true
        if (TextUtils.isEmpty(input)) {
            inputView.isSelected = true
            errorView.visibility = View.VISIBLE
            errorView.text =
                DynamicResourceManager.getString(errorView.context, hintResId)
            result = false
        } else {
            if (!TextUtils.isEmpty(countryCode)) {
                when (countryCode) {
                    CommonConstants.Value.CHINA_CODE, CommonConstants.Value.HK_CODE -> {
                        if (!RegexUtils.match(input, RegexUtils.LETTER_REGEX)) {
                            inputView.isSelected = true
                            errorView.visibility = View.VISIBLE
                            errorView.text =
                                DynamicResourceManager.getString(errorView.context, errorResId)
                            result = false
                        } else {
                            inputView.isSelected = false
                            errorView.visibility = View.INVISIBLE
                        }
                    }

                    else -> {
                        inputView.isSelected = false
                        errorView.visibility = View.INVISIBLE
                    }
                }

            } else {
                inputView.isSelected = false
                errorView.visibility = View.INVISIBLE
            }
        }
        return result
    }

    @JvmStatic
    fun checkChineseInput(
        countryCode: String,
        input: String,
        inputView: EditText,
        errorView: TextView,
        @StringRes hintResId: Int,
        @StringRes errorResId: Int,
    ): Boolean {
        var result = true
        if (TextUtils.isEmpty(input)) {
            inputView.isSelected = true
            errorView.visibility = View.VISIBLE
            errorView.text =
                DynamicResourceManager.getString(errorView.context, hintResId)
            result = false
        } else {
            if (!Utils.isContainChinese(input)
                && CommonConstants.Value.CHINA_CODE == countryCode
            ) {
                inputView.isSelected = true
                errorView.visibility = View.VISIBLE
                errorView.text =
                    DynamicResourceManager.getString(errorView.context, errorResId)
                result = false
            } else {
                inputView.isSelected = false
                errorView.visibility = View.INVISIBLE
            }
        }
        return result
    }

    @JvmStatic
    fun checkIdCardInput(
        input: String,
        countryCode: String,
        inputView: EditText,
        errorView: TextView,
        @StringRes hintResId: Int,
        @StringRes errorResId: Int
    ): Boolean {
        var result = true
        if (TextUtils.isEmpty(input)) {
            inputView.isSelected = true
            errorView.visibility = View.VISIBLE
            errorView.text =
                DynamicResourceManager.getString(errorView.context, hintResId)
            result = false
        } else {
            if (!TextUtils.isEmpty(countryCode)) {
                when (countryCode) {
                    CommonConstants.Value.CHINA_CODE -> {
                        //中国大陆身份证验证
                        if (!RegexUtils.match(input, RegexUtils.CHINESE_ID_CARD_REGEX)) {
                            LogUtils.d("中国大陆身份证验证失败")
                            errorView.visibility = View.VISIBLE
                            inputView.isSelected = true
                            errorView.text =
                                DynamicResourceManager.getString(errorView.context, errorResId)
                            result = false
                        } else {
                            inputView.isSelected = false
                            errorView.visibility = View.INVISIBLE
                        }
                    }

                    CommonConstants.Value.TW_CODE -> {
                        //台湾的身份证验证
                        if (!RegexUtils.match(input, RegexUtils.TW_ID_CARD_REGEX)) {
                            LogUtils.d("台湾的身份证验证失败")
                            errorView.visibility = View.VISIBLE
                            inputView.isSelected = true
                            errorView.text =
                                DynamicResourceManager.getString(errorView.context, errorResId)
                            result = false
                        } else {
                            inputView.isSelected = false
                            errorView.visibility = View.INVISIBLE
                        }
                    }

                    else -> {
                        inputView.isSelected = false
                        errorView.visibility = View.INVISIBLE
                    }
                }
            } else {
                inputView.isSelected = false
                errorView.visibility = View.INVISIBLE
            }
        }
        return result
    }

    @JvmStatic
    fun checkPhoneInput(
        input: String,
        areaCode: String,
        regex: String,
        inputView: EditText,
        errorView: TextView,
        @StringRes hintResId: Int,
        @StringRes errorResId: Int
    ): Boolean {
        //1.空校验
        if (TextUtils.isEmpty(input)) {
            inputView.isSelected = true
            errorView.visibility = View.VISIBLE
            errorView.text =
                DynamicResourceManager.getString(errorView.context, hintResId)
            return false
        }
        //1.google 校验
        val phoneNumberUtil = PhoneNumberUtil.getInstance()
        try {
            val phoneNumber = phoneNumberUtil.parse("$areaCode$input", null)
            if (!phoneNumberUtil.isValidNumber(phoneNumber)) {
                LogUtils.d("google校验失败")
                errorView.visibility = View.VISIBLE
                inputView.isSelected = true
                errorView.text =
                    DynamicResourceManager.getString(errorView.context, errorResId)
                return false
            }
        } catch (e: Exception) {
            LogUtils.d("google校验error:${e.message}")
            errorView.visibility = View.VISIBLE
            inputView.isSelected = true
            errorView.text =
                DynamicResourceManager.getString(errorView.context, errorResId)
            return false
        }
        //2.接口校验
        if (!TextUtils.isEmpty(regex)
            && !RegexUtils.match(input, regex)
        ) {
            LogUtils.d("接口校验失败")
            errorView.visibility = View.VISIBLE
            inputView.isSelected = true
            errorView.text =
                DynamicResourceManager.getString(errorView.context, errorResId)
            return false
        }
        //3.本地规则校验
//        val key = areaCode.replace("+", "").toInt()
//        val isValid = RegexUtils.mobilePhonesByCountry[key]?.let { localRegex ->
//            RegexUtils.match("$key$input", localRegex)
//        } ?: true
//        if (!isValid) {
//            LogUtils.d("本地规则校验失败")
//            errorView.visibility = View.VISIBLE
//            inputView.isSelected = true
//            errorView.text =
//                DynamicResourceManager.getString(errorView.context, errorResId)
//            return false
//        }
        errorView.visibility = View.INVISIBLE
        inputView.isSelected = false
        return true
    }
}